package com.tcwlkj.uuService.ui.set;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.BindingBankModel;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.ui.BaseActivity;
import com.tcwlkj.uuService.utils.BankInfo;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;
import com.tcwlkj.uuService.widget.XEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/18 0018.
 */

public class BindBankCard extends BaseActivity implements TextWatcher {
    @Bind(R.id.edit_cardname_bind01)
    EditText edit_cardname_bind01;
    @Bind(R.id.edit_cardnum_bind01)
    XEditText edit_cardnum_bind01;
    @Bind(R.id.btn_next_bind01)
    Button btn_next_bind01;
    @Bind(R.id.et_bank_type)
    EditText et_bank_type;
    @Bind(R.id.et_bank_name)
    EditText et_bank_name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank_card);
        ButterKnife.bind(this);
        changeTitle("绑定银行卡");
        btn_next_bind01.setClickable(false);
        ininview();
    }
    private void ininview() {
        edit_cardnum_bind01.setRightMarkerDrawable(null);
        //            银行卡号  长度不一定
        edit_cardnum_bind01.setMaxLength(19 + 4);
        edit_cardnum_bind01.setSeparator("-");
        edit_cardnum_bind01.setPattern(new int[]{4, 4, 4, 4, 3});
        edit_cardname_bind01.addTextChangedListener(this);
        edit_cardnum_bind01.addTextChangedListener(this);
        et_bank_type.addTextChangedListener(this);
        et_bank_name.addTextChangedListener(this);
        et_bank_type.setKeyListener(null);
        Intent intent=getIntent();
        if(intent!=null){
            edit_cardname_bind01.setText(intent.getStringExtra("name"));
            edit_cardnum_bind01.setText(intent.getStringExtra("number"));
            et_bank_name.setText(intent.getStringExtra("adress"));
        }
    }

    @OnClick(R.id.btn_next_bind01)
    public  void OnClick(View v){
        switch (v.getId()){
            case R.id.btn_next_bind01:
                String  sub_str_card=edit_cardnum_bind01.getText().toString().trim().replaceAll("-","");
                if(!Tools.checkBankCard(sub_str_card)){
                    ToastUtil.getInstance().show("银行卡号不正确，请绑定正确的银行卡号");
                    return;
                }else if(!BankInfo.getNameOfBank(sub_str_card.toCharArray(),0).substring(0,4).equals("招商银行")){
                    ToastUtil.getInstance().show("非招商银行银行卡，不能进行提现");
                    return;
                }
                bindingBank();
            break;
        }
    }

    private void bindingBank() {
        String name=edit_cardname_bind01.getText().toString().trim();
        String number=edit_cardnum_bind01.getText().toString().trim().replaceAll("-","");
        String bank_type=et_bank_type.getText().toString().trim();
        String bank_adress=et_bank_name.getText().toString().trim();
        Call<CommonResponse<BindingBankModel>> httpResultCall = RetrofitUtils.getRequest(this, true).append(Long.parseLong(PreferencesUtils.getString(MyApp.getContext(),"userID")),1,name,number,"银行",bank_type,bank_adress);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<BindingBankModel>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<BindingBankModel> commonResponse= (CommonResponse<BindingBankModel>) t;
                ToastUtil.getInstance().show("绑定成功");
                Intent mIntent = new Intent();
                ChcheUtils.getInstance().getDriverDetailModel().getCard().setAddress(commonResponse.getData().getBankAddress());
                ChcheUtils.getInstance().getDriverDetailModel().getCard().setNumber(commonResponse.getData().getNumber());
                ChcheUtils.getInstance().getDriverDetailModel().getCard().setType(commonResponse.getData().getBankType());
                ChcheUtils.getInstance().getDriverDetailModel().getCard().setName(commonResponse.getData().getName());

                setResult(2,mIntent);
                finish();
            }

            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    bindingBank();
                }else {
                    ToastUtil.getInstance().show(message);
                }

            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String edit_cardname=edit_cardname_bind01.getText().toString().trim();
        String edit_cardnum=edit_cardnum_bind01.getText().toString().trim();
        String et_bank_type_=et_bank_type.getText().toString().trim();
        String et_bank_name_=et_bank_name.getText().toString().trim();
        if(!TextUtils.isEmpty(edit_cardname)&&!TextUtils.isEmpty(edit_cardnum)&&!TextUtils.isEmpty(et_bank_type_)&&!TextUtils.isEmpty(et_bank_name_)){
            btn_next_bind01.setClickable(true);
            btn_next_bind01.setBackgroundResource(R.drawable.bg_btn_mainred);
        }else{
            btn_next_bind01.setClickable(false);
            btn_next_bind01.setBackgroundResource(R.drawable.bg_btn_gray);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
